const db = require("../../dbutil/index");
const sql = require("../../dbutil/sql");
const token = require("../../util/token");

module.exports = {

    // 
    getUsers: (req, res) => {
        db.query(sql.userManger.getUsers, [], (err, results) => {
            if (err) res.send({ code: 505, msg: "数据库执行错误" });
            else res.send({ code: 200, msg: "成功拿到用户数据", data: results })
        })
    },



    // 冻结账号
    freezeAccount: (req, res) => {
        let { uid } = req.body;
        console.log(uid);
        db.query(sql.userManger.freezeAccount, [uid], (err, results) => {
            if (err) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                if (results.affectedRows > 0) {

                    res.send({ code: 200, msg: "账号冻结成功" });
                } else {
                    res.send({ code: 404, msg: "未找到当前用户" })
                }
            }
        })
    },


    // 恢复用户
    recoverAccount: (req, res) => {
        let { uid } = req.body;
        db.query(sql.userManger.recoverAccount, [uid], (err, results) => {
            if (err) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                if (results.affectedRows > 0) {
                    res.send({ code: 200, msg: "账号恢复成功" });
                } else {
                    res.send({ code: 404, msg: "未找到当前用户" });
                }
            }
        })
    },

    // 获取根据每条新闻id来获取用户评论用
    getNesComments: (req, res) => {
        db.query(sql.commManger.getNesComments, [], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: "数据库执行错误" });
            } else {
                res.send({ code: 200, msg: "评论获取成功",data:results })
            }
        })
    },


    // 删除用户评论
    deleteUserComments: (req, res) => {
        let { comments_id } = req.body;
        console.log(comments_id);
        db.query(sql.commManger.deleteUserComments, [comments_id], (err, results) => {
            console.log(err);
            if (err) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                console.log(results);
                if (results.affectedRows === 0) {
                    res.send({ code: 404, msg: "没有找到当前评论" });
                } else {
                    res.send({ code: 200, msg: "评论删除成功" });
                }
            }
        })
    },

    // 发布新闻
    postNews: (req, res) => {
        let { uid, category_id, title, digest, source, content } = req.body;
        let { filename } = req.files[0];
        let news_id = Date.now();
        let img = `http://127.0.0.1:3000/newsImg/${filename}`
        console.log(filename);
        console.log(img);

        db.query(sql.newsManger.postNews, [news_id, uid, category_id, title, img, digest, source, Date.now()], (err1, results1) => {
            // console.log(err1);
            if (err1) {
                res.send({ code: 505, msg: "数据库执行错误" });
            } else {
                if (results1.affectedRows > 0) {
                    let full_content = `[{"type":"text","content":"${content}"},
                    {"type":"img","imageUrl":"${img}"}]`
                    db.query(sql.newsManger.postNewsDetail, [news_id, full_content], (err2, results2) => {
                        console.log(err2);
                        if (err2) {
                            res.send({ code: 505, msg: "数据库执行错误" });
                        } else {
                            if (results2.affectedRows > 0) {
                                res.send({ code: 200, msg: "新闻详情发布成功" });
                            } else {
                                res.send({ code: 201, msg: "新闻详情发布失败" });
                            }
                        }
                    })
                } else {
                    res.send({ code: 401, msg: "新闻列表插入错误" })
                }
            }
        })
    },



    // 删除新闻
    deleteNews: (req, res) => {
        let { news_id } = req.body;
        db.query(sql.newsManger.deleteNews, [news_id], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: "数据库执行失败" });
            } else {
                if (results.affectedRows === 0) {
                    res.send({ code: 400, msg: "没有找到这条新闻" })
                } else {
                    res.send({ code: 200, msg: "删除成功" })
                }
            }
        })
    },


    // 修改新闻
}